#include "myregister.h"
#include "ui_myregister.h"

myregister::myregister(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::myregister)
{
    ui->setupUi(this);
}

myregister::~myregister()
{
    delete ui;
}

void myregister::on_btnCancel_clicked()
{
    this->reject();
}


void myregister::on_btnOk_clicked()
{
    QString account = ui->editAccount->text().simplified();
    QString pswd = ui->editPswd->text();
    QString repswd = ui->editRePswd->text();
    QString name = ui->editName->text();
    if(account.isEmpty() || pswd.isEmpty() || repswd.isEmpty()){//如果账号,密码,确认密码中有空项
        QMessageBox::warning(0,"警告","请输入完整的信息.");
        return;
    }

    QSqlQuery query;
    query.exec("select * from tb_admin where account='"+account+"';");
    if(query.size() != 0){//如果账号和某个管理员账号相同
        QMessageBox::warning(0,"警告","该账号已经存在.");
        return;
    }

    query.exec("select * from tb_user where account='"+account+"';");
    if(query.size() != 0){//如果账号和某个用户账号相同
        QMessageBox::warning(0,"警告","该账号已经存在.");
        return;
    }

    if(pswd != repswd){//如果两次密码输入不同
        QMessageBox::warning(0,"警告","密码确认错误.");
        return;
    }

    if(query.exec("insert into tb_user(account,pswd,name) values('"+account+"','"+pswd+"','"+name+"');") == false){//如果注册失败
        QMessageBox::warning(0,"警告","账号注册失败.");
        return;
    }

    QMessageBox::information(0,"提示","账号注册成功!");
    this->accept();
}

